package com.fr.schedule.dao;

import com.fr.base.ReflectionUtils;
import com.fr.data.core.db.dialect.OracleDialect;
import com.fr.data.core.db.dml.Insert;
import com.fr.data.core.db.dml.Table;
import com.fr.data.core.db.dml.Update;
import com.fr.data.core.db.dml.Where;
import java.math.BigDecimal;
import java.sql.ResultSet;

public class ObjectMapper
{
  private String tableName;
  private FieldMapper[] fieldMappers;

  public ObjectMapper(String paramString, FieldMapper[] paramArrayOfFieldMapper)
  {
    setTableName(paramString);
    setFieldMappers(paramArrayOfFieldMapper);
  }

  public void setTableName(String paramString)
  {
    this.tableName = paramString;
  }

  public String getTableName()
  {
    return this.tableName;
  }

  public void setFieldMappers(FieldMapper[] paramArrayOfFieldMapper)
  {
    this.fieldMappers = paramArrayOfFieldMapper;
  }

  public FieldMapper[] getFieldMappers()
  {
    return this.fieldMappers;
  }

  public String createSelectAllSQL()
  {
    StringBuffer localStringBuffer = new StringBuffer();
    localStringBuffer.append("SELECT ");
    for (int i = 0; i < this.fieldMappers.length; ++i)
    {
      localStringBuffer.append(this.fieldMappers[i].getColumnName());
      if (i == this.fieldMappers.length - 1)
        localStringBuffer.append(" ");
      else
        localStringBuffer.append(", ");
    }
    localStringBuffer.append("FROM ");
    localStringBuffer.append(getTableName());
    return localStringBuffer.toString();
  }

  public String createDeleteAllSQL()
  {
    return "DELETE FROM " + getTableName();
  }

  public String createDeleteByIdSQL(long paramLong)
  {
    return createDeleteAllSQL() + " WHERE id=" + paramLong;
  }

  public String deleteByObject(Object paramObject)
  {
    for (int i = 0; i < this.fieldMappers.length; ++i)
      if (this.fieldMappers[i] instanceof IdFieldMapper)
      {
        Object localObject = ReflectionUtils.getPrivateFieldValue(paramObject, this.fieldMappers[i].getFieldName());
        ((IdFieldMapper)this.fieldMappers[i]).delete(localObject);
      }
    long l = ((Number)ReflectionUtils.getPrivateFieldValue(paramObject, "id")).longValue();
    return createDeleteByIdSQL(l);
  }

  public String createLoadSQL(long paramLong)
  {
    return createSQLByFiledValue("id", new Long(paramLong));
  }

  public String createSQLByFiledValue(String paramString, Object paramObject)
  {
    StringBuffer localStringBuffer = new StringBuffer();
    localStringBuffer.append("SELECT ");
    for (int i = 0; i < this.fieldMappers.length; ++i)
    {
      localStringBuffer.append(this.fieldMappers[i].getColumnName());
      if (i == this.fieldMappers.length - 1)
        localStringBuffer.append(" ");
      else
        localStringBuffer.append(", ");
    }
    localStringBuffer.append("FROM ");
    localStringBuffer.append(getTableName() + " ");
    String str = findColumnNameByFieldName(paramString);
    localStringBuffer.append("WHERE " + str + "=");
    localStringBuffer.append(paramObject.toString());
    return localStringBuffer.toString();
  }

  public Insert createInsert(Object paramObject, boolean paramBoolean)
  {
    Insert localInsert = new Insert(new Table(getTableName()));
    for (int i = 0; i < this.fieldMappers.length; ++i)
    {
      String str = this.fieldMappers[i].getColumnName();
      int j = this.fieldMappers[i].getType();
      Object localObject = ReflectionUtils.getPrivateFieldValue(paramObject, this.fieldMappers[i].getFieldName());
      if (("id".equals(this.fieldMappers[i].getFieldName())) && (isNegative(localObject)))
      {
        if (paramBoolean)
          localInsert.putNameExpression("id", OracleDialect.getOracleIncNextValue(getTableName()));
      }
      else
      {
        if (localObject == null)
          break label206:
        if (this.fieldMappers[i] instanceof IdFieldMapper)
          localObject = new Long(((IdFieldMapper)this.fieldMappers[i]).field2Id(localObject));
        else if (this.fieldMappers[i] instanceof ValueFieldMapper)
          localObject = ((ValueFieldMapper)this.fieldMappers[i]).field2Value(localObject);
        localInsert.addColumn(str, j, localObject);
      }
    }
    label206: return ((Insert)localInsert);
  }

  private static boolean isNegative(Object paramObject)
  {
    if (paramObject instanceof Long)
      return (((Long)paramObject).longValue() < 0L);
    if (paramObject instanceof Integer)
      return (((Integer)paramObject).intValue() < 0);
    if (paramObject instanceof String)
      return (Long.valueOf((String)paramObject).longValue() < 0L);
    return false;
  }

  public Update createUpdate(Object paramObject)
  {
    Update localUpdate = new Update(new Table(getTableName()));
    for (int i = 0; i < this.fieldMappers.length; ++i)
    {
      if ("id".equals(this.fieldMappers[i].getColumnName()))
        break label155:
      Object localObject = ReflectionUtils.getPrivateFieldValue(paramObject, this.fieldMappers[i].getFieldName());
      if (this.fieldMappers[i] instanceof IdFieldMapper)
        localObject = new Long(((IdFieldMapper)this.fieldMappers[i]).field2Id(localObject));
      else if (this.fieldMappers[i] instanceof ValueFieldMapper)
        localObject = ((ValueFieldMapper)this.fieldMappers[i]).field2Value(localObject);
      localUpdate.addColumn(this.fieldMappers[i].getColumnName(), this.fieldMappers[i].getType(), localObject);
    }
    label155: Where localWhere = new Where();
    localWhere.addColumn("id", 4, 0, ReflectionUtils.getPrivateFieldValue(paramObject, "id"));
    localUpdate.setWhere(localWhere);
    return ((Update)localUpdate);
  }

  public Object extractResultSet(ResultSet paramResultSet, Class paramClass)
    throws Exception
  {
    Object localObject1 = paramClass.newInstance();
    for (int i = 0; i < this.fieldMappers.length; ++i)
    {
      Object localObject2 = null;
      if (this.fieldMappers[i].getType() == 12)
        localObject2 = paramResultSet.getString(this.fieldMappers[i].getColumnName());
      else
        localObject2 = paramResultSet.getObject(this.fieldMappers[i].getColumnName());
      if ((this.fieldMappers[i] instanceof IdFieldMapper) && (localObject2 != null))
        localObject2 = ((IdFieldMapper)this.fieldMappers[i]).id2Field(((Number)localObject2).longValue());
      else if (this.fieldMappers[i] instanceof ValueFieldMapper)
        localObject2 = ((ValueFieldMapper)this.fieldMappers[i]).value2Field(localObject2);
      if (localObject2 == null)
        break label228:
      if ((localObject2 instanceof BigDecimal) && (((this.fieldMappers[i].getType() == -5) || (this.fieldMappers[i].getType() == 4))))
        localObject2 = new Long(((BigDecimal)localObject2).longValue());
      ReflectionUtils.setPrivateFieldValue(localObject1, this.fieldMappers[i].getFieldName(), localObject2);
    }
    label228: return localObject1;
  }

  private String findColumnNameByFieldName(String paramString)
  {
    for (int i = 0; i < this.fieldMappers.length; ++i)
      if (paramString.equals(this.fieldMappers[i].getFieldName()))
        return this.fieldMappers[i].getColumnName();
    return null;
  }
}